using System;
using System.Linq;
using LegoBotPrima.Actions;
using LegoBotPrimaAbstractions;
using NUnit.Framework;

namespace LegoBotPrimaTests
{
    [TestFixture]
    public class BotAction_Fixture : BaseFixture
    {
        [Test]
        public void Linked_List_Of_Previous_Actions_Does_Not_Get_Longer_Than_What_Is_Allowed()
        {
            const int MAX = 10;
            const int MORE = 5;
            Config.Instance = GetMock<ILegoBotPrimaConfig>(m => m.SetupGet(c => c.MaxNumberPreviousActionsKept).Returns(MAX));

            TestAction action = null;

            foreach (var i in Enumerable.Range(0, MAX + MORE))
            {
                var prev = action;
                action = new TestAction{ PreviousAction = prev };
            }

            Assert.That(action.Count(), Is.EqualTo(MAX));

            Config.ResetDefault();
        }


        [Test]
        public void Linked_List_Of_Previous_Actions_Assignes_Generation_Number_As_Expected()
        {
            const int MAX = 10;
            const int MORE = 5;
            Config.Instance = GetMock<ILegoBotPrimaConfig>(m => m.SetupGet(c => c.MaxNumberPreviousActionsKept).Returns(MAX));

            TestAction action = null;

            foreach (var i in Enumerable.Range(0, MAX + MORE))
            {
                var prev = action;
                action = new TestAction { PreviousAction = prev };
            }
            var history = action.ToArray();

            foreach (var i in Enumerable.Range(0, history.Length))
            {
                Assert.That(history[i].Generation, Is.EqualTo(MAX + MORE - i));
            }
            Config.ResetDefault();
        }

        // no specific implementation is required for this test
        public class TestAction : BotAction
        {
            public override string ToString()
            {
                return "{Generation:"+Generation+"}";
            }
        }
    }
}